﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
namespace _5ChickDeal.UserControl
{
    public partial class LastDealList : System.Web.UI.UserControl
    {
        DataClassesDataContext db;
        int NumberLinePerPage = 2;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DataClassesDataContext();
            int p;
            var Source = from deal in db.Deals
                         select new
                         {
                             id = deal.id,
                             ten = deal.ten,
                             daban = deal.DonHangs.Sum(dh =>dh.soluong),
                             giatri = deal.gia,
                             giaban = deal.giaconlai,
                             giam = 100-(int)(((float)deal.giaconlai/deal.gia)*100),
                             hinh = deal.HinhAnhs.First()

                         };


            int count = Source.Count(a=>true);

            int NumberPage = (int)Math.Round(((float)count / NumberLinePerPage + 0.5));

            
            try
            {
                p = int.Parse(Request.QueryString["p"]);
                if (p < 0 || p > count)
                    p = 1;

            }
            catch
            {
                p = 1;
            }

            try
            {
                var FinalSource = Source.Skip((p - 1) * NumberLinePerPage).Take(NumberLinePerPage);
                Repeater1.DataSource = FinalSource;
                Repeater1.DataBind();
            }
            catch { }
            String html = "";
            if (count > 1)
            {
                for (int i = 1; i <= NumberPage; i++)
                {
                    if (i == p)
                    {
                        html += "<li class='Active'>" + i.ToString() + "</li>";
                    }
                    else
                    {
                        html += "<li><a href='DealList.aspx?p=" + i.ToString() + "'>" + i.ToString() + "</a></li>";
                    }
                }
            }
            UlPage.InnerHtml = html;
            

        }
    }
}